package com.rocksdata.mc.ui.activity;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.ctz.commonframe.ui.TitleBarActivity;
import com.ctz.commonframe.ui.view.dslv.DragSortListView;
import com.ctz.commonframe.ui.widget.AbsAdapter;
import com.ctz.commonframe.ui.widget.AdapterViewHolder;
import com.ctz.commonframe.utils.SharedPrefrencesUtil;
import com.rocksdata.mc.SharedPreferencesKey;
import com.rocksdata.mc.business.UserInfoManager;
import com.rocksdata.mc.entity.AppWidgetInfo;
import com.rocksdata.mc.R;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 陈堂正 on 2017/2/7.
 */

@ContentView(R.layout.activity_widget_manager)
public class WidgetManagerActivity extends TitleBarActivity {
    @ViewInject(R.id.lv_widget)
    private DragSortListView lvWidget;

    private boolean isVisitor;
    private List<AppWidgetInfo> widgetInfoList = new ArrayList<>();
    private WidgetListAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setTitleMiddleText("组件管理");
        getAppWidgetList();
        adapter = new WidgetListAdapter(this, widgetInfoList);
        lvWidget.setAdapter(adapter);
        lvWidget.setDragEnabled(true);
        lvWidget.setDropListener(new DragSortListView.DropListener() {
            @Override
            public void drop(int from, int to) {
                if (from != to) {
                    AppWidgetInfo item = adapter.getItem(from);
                    widgetInfoList.remove(from);
                    widgetInfoList.add(to, item);
                    saveWidgetList();
                    adapter.notifyDataSetChanged();
                }
            }
        });
    }

    private void getAppWidgetList(){
        isVisitor = UserInfoManager.isVisitor();
        String typeStr = (String) SharedPrefrencesUtil.get(
                isVisitor ? SharedPreferencesKey.KEY_VISITOR_WIDGET
                        : SharedPreferencesKey.KEY_USER_WIDGET, "");

        widgetInfoList = JSON.parseArray(typeStr, AppWidgetInfo.class);
    }

    private class WidgetListAdapter extends AbsAdapter<AppWidgetInfo>{

        public WidgetListAdapter(Context context, List<AppWidgetInfo> datas) {
            super(context, datas, R.layout.item_widget_list);
        }

        @Override
        public void convert(AdapterViewHolder holder, final AppWidgetInfo data) {
            ImageView imageView = holder.getView(R.id.iv_widget_manager);
            TextView tvName = holder.getView(R.id.tv_widget_name);
            imageView.setVisibility(data.id == AppWidgetInfo.TYPE_APP || data.id == AppWidgetInfo.TYPE_WIDGET_MANAGER ? View.INVISIBLE : View.VISIBLE);
            imageView.setImageResource(data.isOpen ? R.mipmap.ic_remove : R.mipmap.ic_app_add);
            imageView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    data.isOpen = !data.isOpen;
                    saveWidgetList();
                    notifyDataSetChanged();
                }
            });
            tvName.setText(data.name + " 组件");
        }
    }

    private void saveWidgetList(){
        String types = JSON.toJSONString(widgetInfoList);
        SharedPrefrencesUtil.put(
                isVisitor ? SharedPreferencesKey.KEY_VISITOR_WIDGET
                        : SharedPreferencesKey.KEY_USER_WIDGET, types);
    }

}
